// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StrategyPluginTrackingCell.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using System.Threading;

    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The strategy plugin tracking cell.
    /// </summary>
    public class StrategyPluginTrackingCell : IStrategyPluginTrackingCell
    {
        private static readonly Identity Identity = Identity.Create(System.Reflection.MethodBase.GetCurrentMethod());

        #region Constants and Fields

        /// <summary>
        /// Gets or sets State.
        /// </summary>
        private EStrategyState state;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StrategyPluginTrackingCell"/> class.
        /// </summary>
        /// <param name="strategyService">
        /// The strategyService.
        /// </param>
        /// <param name="plugin">
        /// The plugin.
        /// </param>
        public StrategyPluginTrackingCell(IStrategyService strategyService, IStrategyPlugin plugin)
        {
            this.StrategyService = strategyService;
            this.StrategyPlugin = plugin;
            this.State = EStrategyState.Destroyed;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets a value indicating whether IsCreated.
        /// </summary>
        public bool IsCreated
        {
            get
            {
                return this.State == EStrategyState.Created;
            }
        }

        /// <summary>
        /// Gets a value indicating whether IsCreating.
        /// </summary>
        public bool IsCreating
        {
            get
            {
                return this.State == EStrategyState.Creating;
            }
        }

        /// <summary>
        /// Gets a value indicating whether IsDestroyed.
        /// </summary>
        public bool IsDestroyed
        {
            get
            {
                return this.State == EStrategyState.Destroyed;
            }
        }

        /// <summary>
        /// Gets a value indicating whether IsDestroying.
        /// </summary>
        public bool IsDestroying
        {
            get
            {
                return this.State == EStrategyState.Destroying;
            }
        }

        /// <summary>
        /// Gets Referential.
        /// </summary>
        public IReferential Referential
        {
            get
            {
                return Singleton<Universe>.Instance.Referential(this);
            }
        }

        /// <summary>
        /// Gets or sets State.
        /// </summary>
        public EStrategyState State
        {
            get
            {
                return this.state;
            }

            set
            {
                this.state = value;
                Thread.Sleep(333);
            }
        }

        /// <summary>
        /// Gets StrategyPlugin.
        /// </summary>
        public IStrategyPlugin StrategyPlugin { get; private set; }

        /// <summary>
        /// Gets or sets StrategyService.
        /// </summary>
        private IStrategyService StrategyService { get; set; }

        #endregion

        #region Implemented Interfaces

        #region IStrategyPluginTrackingCell

        /// <summary>
        /// The create.
        /// </summary>
        /// <returns>
        /// </returns>
        public Status Create()
        {
            if (this.IsCreating)
            {
                return Status.Failure(Identity, "Already creating");
            }

            if (this.IsCreated)
            {
                return Status.Failure(Identity, "Already created");
            }

            if (this.IsDestroying)
            {
                return Status.Failure(Identity, "Currently destroying");
            }

            // "Destroyed" -> "Creating"
            this.State = EStrategyState.Creating;

            // Prepare the referential
            this.Referential.Startup();

            // Set the referential
            (this.StrategyPlugin as StrategyPlugin).Referential = this.Referential;

            // Initiate create on the plugin
            var status = this.StrategyPlugin.Create();

            // Did it work ?
            if (status)
            {
                // "Creating" -> "Created"
                this.State = EStrategyState.Created;
                return Status.Success(Identity);
            }

            // It didn't work, return to death (is this the frost of Purcell's King Arthur?)
            return this.Destroy();
        }

        /// <summary>
        /// The destroy.
        /// </summary>
        /// <returns>
        /// </returns>
        public Status Destroy()
        {
            if (this.IsDestroying)
            {
                return Status.Failure(Identity, "Already destroying");
            }

            if (this.IsDestroyed)
            {
                return Status.Failure(Identity, "Already destroyed");
            }

            // "Creating" or "Created" -> "Destroying"
            this.State = EStrategyState.Destroying;

            // Initiate destroy on plugin only if fully created
            if (this.IsCreated)
            {
                // Initiate the destroy on the plugin
                this.StrategyPlugin.Destroy();
            }

            // Shutdown the referential
            this.Referential.Shutdown();

            // Unset the referential
            (this.StrategyPlugin as StrategyPlugin).Referential = null;

            // "Destroying" -> "Destroyed"
            this.State = EStrategyState.Destroyed;

            // All done
            return Status.Success(Identity);
        }

        #endregion

        #endregion
    }
}